import React from 'react'
import { Notify } from 'react-vant'
import { Button, Input, Form } from 'react-vant'
import { login } from '../../../api/my'
import { useNavigate } from 'react-router-dom'
import { useAppDispatch } from '../../../app/hooks'
import { loginSuccess } from '../../../app/modules/user'

export default () => {
  const [form] = Form.useForm()
  const navigate = useNavigate()
  const dispatch = useAppDispatch()
  const onFinish = async values => {
    const resp = await login(values);
    const { status, result } = resp.data;
    if(status === '0000') {
      // 进入首页
      navigate('/')
      // 存储信息
      dispatch(loginSuccess(result))
      // 友好提示
      Notify.show({ type: 'success', message: '登录成功， 欢迎用户: ' + result.nickName })
    }
  }

  return (
    <Form
      form={form}
      onFinish={onFinish}
      initialValues={{
        email: '906446244@qq.com',
        pwd: '123456'
      }}
      footer={
        <div style={{ margin: '16px 16px 0' }}>
          <Button round nativeType='submit' type='primary' block>
            提交
          </Button>
        </div>
      }
    >
      <Form.Item
        tooltip={{
          message:
            'A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself.',
        }}
        intro='确保这是唯一的用户名'
        rules={[{ required: true, message: '请填写用户名' }]}
        name='email'
        label='用户名'
      >
        <Input placeholder='请输入用户名'/>
      </Form.Item>
      <Form.Item
        rules={[{ required: true, message: '请填写密码' }]}
        name='pwd'
        label='密码'
      >
        <Input placeholder='请输入密码' />
      </Form.Item>
    </Form>
  )
}
